﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.CApAWCMfgTcServices
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.Viewers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using Tecnomatix.Engineering;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.TcCommands;

internal class CApAWCMfgTcServices
{
  private List<CApAWCMfgObject> m_cachedObjects = new List<CApAWCMfgObject>();
  private Dictionary<string, string> m_windowTagsToViewId = new Dictionary<string, string>();
  internal CApAWCMfgCache m_mfgCache;

  internal CApAWCMfgTcServices() => this.m_mfgCache = new CApAWCMfgCache(this);

  public TxTcImportResultEx LoadMfgs(
    IEnumerable<string> mfgsToLoad,
    bool forPreview,
    ITxObject targetOperation)
  {
    TxTcImportResultEx tcImportResultEx = (TxTcImportResultEx) null;
    if (mfgsToLoad != null && mfgsToLoad.Count<string>() > 0)
    {
      List<TxTcImportObjectDataEx> importList = this.GenerateImportList(mfgsToLoad.ToList<string>());
      if (importList.Count > 0)
      {
        TxOperationTreeInternal operationTreeInternal = new TxOperationTreeInternal();
        operationTreeInternal.FreezeTree();
        this.m_mfgCache.IgnoreVisiblityChange = true;
        try
        {
          tcImportResultEx = TxTcDataImporterEx.LoadMfgs(forPreview, (IEnumerable<TxTcImportObjectDataEx>) importList, targetOperation);
        }
        catch (Exception ex)
        {
          TxApplication.LogWriter.WriteExceptionLine(ex);
        }
        finally
        {
          this.m_mfgCache.IgnoreVisiblityChange = false;
          operationTreeInternal.UnFreezeTree();
        }
      }
    }
    return tcImportResultEx;
  }

  public Dictionary<ITxPlanningObject, string> GetBomLineUids(TxObjectList objects)
  {
    Dictionary<ITxPlanningObject, string> bomLineUids = (Dictionary<ITxPlanningObject, string>) null;
    TxObjectList<ITxPlanningObject> txObjectList = new TxObjectList<ITxPlanningObject>(((IEnumerable) ((TxObjectList<ITxObject>) objects).PlanningRepresentations).Cast<ITxPlanningObject>());
    try
    {
      bomLineUids = TxTcUtilities.GetBomLineUidsByPlanningObjects(txObjectList);
    }
    catch (TxException ex)
    {
      TxApplication.LogWriter.WriteExceptionLine((Exception) ex);
    }
    return bomLineUids;
  }

  public Dictionary<string, ITxPlanningObject> GetPlanningObjects(List<string> bomLineUids)
  {
    Dictionary<string, ITxPlanningObject> planningObjects = (Dictionary<string, ITxPlanningObject>) null;
    try
    {
      planningObjects = TxTcUtilities.GetPlanningObjectsByBomLineUids(bomLineUids);
    }
    catch (TxException ex)
    {
      TxApplication.LogWriter.WriteExceptionLine((Exception) ex);
    }
    return planningObjects;
  }

  private List<TxTcImportObjectDataEx> GenerateImportList(List<string> mfgIdsToLod)
  {
    IEnumerable<CApAWCMfgObject> cachedMfgObjects = this.GetCachedMfgObjects(mfgIdsToLod);
    List<TxTcImportObjectDataEx> importList = new List<TxTcImportObjectDataEx>();
    foreach (CApAWCMfgObject capAwcMfgObject in cachedMfgObjects)
    {
      TxTcImportObjectDataEx importObjectDataEx = new TxTcImportObjectDataEx(capAwcMfgObject.CloneStableIdChain, capAwcMfgObject.ViewTag, capAwcMfgObject.Uid);
      importList.Add(importObjectDataEx);
    }
    return importList;
  }

  public IEnumerable<CApAWCMfgObject> GetCachedMfgObjects(List<string> mfgIdsToLod)
  {
    this.CacheMfgObjects(mfgIdsToLod);
    return this.m_cachedObjects.Where<CApAWCMfgObject>((Func<CApAWCMfgObject, bool>) (x => mfgIdsToLod.Contains(x.Uid)));
  }

  private void CacheMfgObjects(List<string> mfgIdsToLod)
  {
    IEnumerable<string> source = mfgIdsToLod.Where<string>((Func<string, bool>) (x => this.m_cachedObjects.FindIndex((Predicate<CApAWCMfgObject>) (y => y.Uid == x)) < 0));
    if (source == null || source.Count<string>() <= 0)
      return;
    List<string> list = source.ToList<string>();
    Dictionary<string, string> viewTagIds = this.GetViewTagIds(list);
    Dictionary<string, string> cloneStableIdChains = this.GetUidsToCloneStableIdChains(list);
    foreach (KeyValuePair<string, string> keyValuePair in viewTagIds)
    {
      string key = keyValuePair.Key;
      string str1 = keyValuePair.Value;
      string str2 = (string) null;
      if (cloneStableIdChains.TryGetValue(key, out str2) && !string.IsNullOrEmpty(str2))
        this.m_cachedObjects.Add(new CApAWCMfgObject()
        {
          Uid = key,
          ViewTag = str1,
          CloneStableIdChain = str2
        });
    }
  }

  public Dictionary<string, string> GetViewTagIds(List<string> mfgUids)
  {
    Dictionary<string, string> viewTagIds = new Dictionary<string, string>();
    Dictionary<string, string> uidsToWindowTagIds = this.GetUidsToWindowTagIds(mfgUids);
    if (uidsToWindowTagIds != null && uidsToWindowTagIds.Count > 0)
    {
      foreach (KeyValuePair<string, string> keyValuePair in uidsToWindowTagIds)
      {
        string str = keyValuePair.Value;
        string key = keyValuePair.Key;
        string viewIdFromWindowTag;
        if (this.m_windowTagsToViewId.ContainsKey(str))
        {
          viewIdFromWindowTag = this.m_windowTagsToViewId[str];
        }
        else
        {
          viewIdFromWindowTag = this.GetViewIdFromWindowTag(str);
          this.m_windowTagsToViewId.Add(str, viewIdFromWindowTag);
        }
        viewTagIds.Add(key, viewIdFromWindowTag);
      }
    }
    return viewTagIds;
  }

  private string GetViewIdFromWindowTag(string windowTag)
  {
    return TxTcUtilities.GetViewIdFromWindowTag(windowTag).ExternalId;
  }

  private TcSoaModelObjectData[] GetBomWindowTagIds(List<string> uids)
  {
    TcSoaModelObjectData[] objects = (TcSoaModelObjectData[]) null;
    if (uids != null && uids.Count > 0)
    {
      TcSoaAssignDataFromTcService o = new TcSoaAssignDataFromTcService();
      ITcSoaAssignDataFromTcService dataFromTcService = (ITcSoaAssignDataFromTcService) o;
      try
      {
        List<string> stringList = new List<string>()
        {
          "bl_window"
        };
        dataFromTcService.GetProperties(uids.ToArray(), stringList.ToArray(), out objects);
      }
      catch (COMException ex)
      {
        TxApplication.LogWriter.WriteErrorLine(ex.Message);
      }
      catch (Exception ex)
      {
        TxApplication.LogWriter.WriteExceptionLine(ex);
      }
      finally
      {
        Marshal.ReleaseComObject((object) o);
      }
    }
    return objects;
  }

  private Dictionary<string, string> GetUidsToWindowTagIds(List<string> uids)
  {
    Dictionary<string, string> uidsToWindowTagIds = new Dictionary<string, string>();
    TcSoaModelObjectData[] bomWindowTagIds = this.GetBomWindowTagIds(uids);
    if (bomWindowTagIds != null)
    {
      foreach (TcSoaModelObjectData soaModelObjectData in bomWindowTagIds)
      {
        string propertyValue = soaModelObjectData.propertyValues[0];
        if (!string.IsNullOrEmpty(propertyValue))
        {
          string str = Convert.ToInt64(propertyValue, 16 /*0x10*/).ToString();
          uidsToWindowTagIds.Add(soaModelObjectData.uid, str);
        }
      }
    }
    return uidsToWindowTagIds;
  }

  private Dictionary<string, string> GetUidsToCloneStableIdChains(List<string> uids)
  {
    Dictionary<string, string> cloneStableIdChains = new Dictionary<string, string>();
    string[] array = uids.ToArray();
    string[] lineChainList = (string[]) null;
    TcSoaAssignDataFromTcService o = new TcSoaAssignDataFromTcService();
    try
    {
      (o as ITcSoaAssignDataFromTcService).ComputeMultipleCloneStableIdChains(array, out lineChainList);
    }
    catch (Exception ex)
    {
      TxApplication.LogWriter.WriteExceptionLine(ex);
    }
    finally
    {
      Marshal.ReleaseComObject((object) o);
    }
    if (lineChainList.Length == array.Length)
    {
      for (int index = 0; index < array.Length; ++index)
      {
        string key = array[index];
        string str = lineChainList[index];
        cloneStableIdChains.Add(key, str);
      }
    }
    return cloneStableIdChains;
  }

  internal void ClearMfgsPreview()
  {
    this.m_mfgCache.ClearPreviewedMfgs();
    TxTcDataImporterEx.ClearPreviewData();
    TxApplication.RefreshDisplay();
  }

  internal static bool ProductScopeIsLoaded() => TxTcSessionInfoEx.ProductScopes.Count > 0;

  internal static bool ProcessOrOperationContainerIsLoaded()
  {
    bool flag = false;
    TxOperationRoot operationRoot = TxApplication.ActiveDocument.OperationRoot;
    if (TxOperationRoot.op_Inequality(operationRoot, (ITxObject) null) && operationRoot.Count > 0)
    {
      TxTypeFilter txTypeFilter = new TxTypeFilter(typeof (ITxTcOperation));
      txTypeFilter.AddIncludedType(typeof (TxTcProcess));
      flag = ((Collection<ITxObject>) operationRoot.GetAllDescendants((ITxTypeFilter) txTypeFilter)).Count > 0;
    }
    return flag;
  }
}
